package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.GroupMemberDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.chat.ChatMessageDto;
import com.lanjiu.lib.imclient.dto.chat.TextDto;
import com.lanjiu.lib.imclient.dto.extra.ChatGroupMsgDto;
import com.lanjiu.lib.imclient.dto.extra.InviteDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.GUID;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.ui.IView;

import java.util.List;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/12/11 19:32
 */
public class InviteFriendView extends IView {

    private InviteFriendCallback mCallback;

    public InviteFriendView(InviteFriendCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doCreateGroup(String userIds) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_CREATE_REGISTERED : EventConstants.Action.GROUP_CREATE_TOURISTS;
        doSendRequest(action, userIds);
    }

    public void doInviteGroup(String groupIds, String userIds) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_MEMBER_INVITE_REGISTERED : EventConstants.Action.GROUP_MEMBER_INVITE_TOURISTS;
        doSendRequest(action, new InviteDto(groupIds, userIds).toJson());
    }

    public void doDeleteGroup(String groupId, String groupName, List<FriendTable> checkedList, String memberId) {
        StringBuilder userIds = new StringBuilder();
        StringBuilder userNicks = new StringBuilder();
        StringBuilder userAvatars = new StringBuilder();
        for (FriendTable table : checkedList) {
            userIds.append(",").append(table.getFriendUserId());
            userNicks.append(",").append(table.getFriendNickname());
            userAvatars.append(",").append(table.getFriendAvatar());
        }
        if(userIds.length() > 0) userIds.deleteCharAt(0);
        if(userNicks.length() > 0) userNicks.deleteCharAt(0);
        if(userAvatars.length() > 0) userAvatars.deleteCharAt(0);
        ChatGroupMsgDto chatGroupMsgDto = new ChatGroupMsgDto();
        GroupMemberDto groupMemberDto = new GroupMemberDto(IMSContacts.UserType.REGISTERED_USER);
        groupMemberDto.setGroupMemberId(memberId);
        groupMemberDto.setGroupMemberUserId(userIds.toString());
        groupMemberDto.setGroupNemberName(userNicks.toString());
        groupMemberDto.setGroupMemberAvatar(userAvatars.toString());
        groupMemberDto.setGroupMemberNick(UserPreferences.getUserId());
        groupMemberDto.setGroupMemberAlias(UserPreferences.getNickname());
        groupMemberDto.setUserType(UserPreferences.getAvatar());
        GroupDto groupDto = new GroupDto(IMSContacts.UserType.REGISTERED_USER);
        groupDto.setGroupId(groupId);
        groupDto.setGroupName(groupName);
        groupDto.setGroupOwnerUserId(UserPreferences.getUserId());
        groupDto.setGroupType(IMSUtils.userTypeAnalysis(groupId));
        groupDto.setSnapchatStatus(false);
        groupMemberDto.setGroup(groupDto);
        chatGroupMsgDto.setGroupMemberDto(groupMemberDto);
        ChatMessageDto chatMessageDto = new ChatMessageDto();
        chatMessageDto.setChatType(IMSContacts.MsgContentType.DELETE_GROUP_MEMBER);
        chatMessageDto.setMsgId(GUID.random());
        TextDto textDto = new TextDto();
        textDto.setContent("移出群聊");
        textDto.setLength(String.valueOf(textDto.getContent().length()));
        chatMessageDto.setText(textDto);
        chatGroupMsgDto.setChatMessageDto(chatMessageDto);
        doSendRequest(EventConstants.Action.GROUP_MEMBER_DELETE, chatGroupMsgDto.toJson());
    }

    @Override
    protected void onMessageEventHandle(MessageEvent event) {
        Logger.print(event.toString());
        if (event.action() == EventConstants.Action.GROUP_CREATE_TOURISTS ||
                event.action() == EventConstants.Action.GROUP_CREATE_REGISTERED) {
            GroupDto group = null;
            if (checkIsSuccess(event.code(), event.data()))
                group = Dto.fromJson(event.data(), GroupDto.class);
            if (null != mCallback)
                mCallback.onCreateGroupCallback(event.code(), event.msg(), group);
        } else if (event.action() == EventConstants.Action.GROUP_MEMBER_INVITE_TOURISTS ||
                event.action() == EventConstants.Action.GROUP_MEMBER_INVITE_REGISTERED) {
            if (null != mCallback)
                mCallback.onInviteGroupCallback(event.code(), event.msg());
        } else if (event.action() == EventConstants.Action.GROUP_MEMBER_DELETE) {
            if (null != mCallback)
                mCallback.onDeleteGroupCallback(event.code(), event.msg());
        }
    }

    public interface InviteFriendCallback {
        void onCreateGroupCallback(int code, String msg, GroupDto data);

        void onInviteGroupCallback(int code, String msg);

        void onDeleteGroupCallback(int code, String msg);
    }
}
